package com.zha.boss.mvc.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zha.boss.constants.SysConstants;
import com.zha.boss.dao.entity.UserInfo;
import com.zha.boss.enums.resp.RespCode;
import com.zha.boss.mvc.convert.UserConvert;
import com.zha.boss.mvc.models.forms.UserEditForm;
import com.zha.boss.mvc.models.resp.AjaxResult;
import com.zha.boss.mvc.models.resp.PageInfo;
import com.zha.boss.mvc.models.vo.UserInfoMatchVo;
import com.zha.boss.mvc.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @Author: zha
 * @CreateTime: 2024年09月11日 17:31
 */
@RestController
public class UserController {

    @Resource
    private UserService userService;
    @Resource
    private UserConvert userConvert;
    /**
     * 分页查询用户列表
     * @param pageNo 页号
     * @return
     */
  @GetMapping("/user/pageList")
  public AjaxResult pageList(Integer pageNo){
      pageNo = (pageNo == null || pageNo < 1)? 1 : pageNo;
      // 分页查询
      IPage<UserInfo> pageResult = userService.pageList(pageNo, SysConstants.PAGE_SIZE);
      // 总页数
      long pages = pageResult.getPages();
      // 分页查询记录
      List<UserInfo> userInfoList = pageResult.getRecords();
      // 创建结果对象
      PageInfo pageInfo = new PageInfo(pageNo,pages);
      Map<String,Object> data = new HashMap<>();
      data.put("page",pageInfo);
      data.put("list",userConvert.mapListUserInfoVo(userInfoList));
      try {
          TimeUnit.SECONDS.sleep(2);
      } catch (InterruptedException e) {
          throw new RuntimeException(e);
      }
      return AjaxResult.ok(data);
  }

    /**
     * @RequestBody : 接收post请求体中的json数据,将数据转为对象类型
     * @param userEditForm
     * @return
     */
    @PostMapping("/user/edit")
    public AjaxResult userRegisterEdit(@RequestBody UserEditForm userEditForm){
        // 调用service处理注册或修改,loginName的值不能修改。
        userService.userRegisterEdit(userEditForm);
        return AjaxResult.ok();
    }

    /**
     * 根据id查询用户信息
     */
    @GetMapping("/user/id")
    public AjaxResult getUserInfo(String id){
        if (StrUtil.isNotBlank(id) && id.trim().length() > 10){
          UserInfo userInfo = userService.queryUserById(id);
          return AjaxResult.ok(userConvert.toUserInfoVo(userInfo));
        }
        return AjaxResult.warn(RespCode.REQUEST_PARAM_ERR);
    }

    /**
     * 编辑用户状态
     */
    @PostMapping("/user/status")
    public AjaxResult userEditStatus(String userNo){
        if (StrUtil.isNotBlank(userNo) && userNo.trim().length() > 10){
            userService.editUserStatus(userNo);
            return AjaxResult.ok();
        }
        return AjaxResult.warn(RespCode.REQUEST_PARAM_ERR);
    }

    /**
     * 模糊查询用户信息
     */
    @GetMapping(value = "/user/match/shopName")
    public AjaxResult matchShopName(String shopName){
        if (StrUtil.isNotBlank(shopName) && shopName.trim().length() > 1){
            List<UserInfo> userInfoList = userService.matchShopName(shopName);
            // user_no,shop_name 两个列数据
            List<UserInfoMatchVo> userInfoMatchVoList = userConvert.mapListUserInfoMatchVo(userInfoList);
            return AjaxResult.ok(userInfoMatchVoList);
        }
        return AjaxResult.warn(RespCode.REQUEST_PARAM_ERR);
    }
}
